package eventHandling.mouseHandling;

import SVGDOM.SVGDOMManipulator;
import java.awt.Color;
import java.awt.Point;
import java.awt.event.MouseEvent;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.event.MouseInputAdapter;
import org.apache.batik.bridge.UpdateManager;
import org.apache.batik.swing.JSVGCanvas;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import svgCanvas.Rectangle.Rectangle;
import svgCanvas.Rectangle.RectangleCreator;

/**
 * This class is used to handle mouse events on the SVG canvas
 * @author Martin
 */
public class SVGCanvasMouseHandler extends MouseInputAdapter {
    private static final String NAMESPACE = "http://www.w3.org/2000/svg";    
    private JSVGCanvas _svgCanvas;
    private JColorChooser _colorChooser;
    private static Point _dragStart;
    private static Node _tempRect;
    private JTextField _anotationTextField;
    private SVGDOMManipulator _domManipulator = new SVGDOMManipulator();
    private JComboBox _shapeChooser;
    private JCheckBox _includeText;
    
    /**
     * 
     * @param canvas is the SVG Canvas we are handling events for
     * @param colorChooser is the JColorChooser component where user is picking anotation color
     * @param anotationTextField is the textField object whichs we take the anotation text from
     */
    public SVGCanvasMouseHandler(JSVGCanvas canvas,
            JColorChooser colorChooser,
            JTextField anotationTextField,
            JComboBox shapeChooser,
            JCheckBox includeText
            )
    {
        _svgCanvas = canvas;
        _colorChooser = colorChooser;
        _anotationTextField = anotationTextField;
        _shapeChooser = shapeChooser;
        _includeText = includeText;
    }

    @Override
    public void mouseClicked(MouseEvent e) 
    {
        UpdateManager updateManager = _svgCanvas.getUpdateManager();
        // fix
        if(updateManager == null) return;
        if(_anotationTextField.getText().equals("Enter annotation text"))
        {
            _anotationTextField.requestFocus();
            return;
        }
        final Point point = e.getPoint();
        final MouseEvent event = e;
        //run the event of updating the canvas
        updateManager.getUpdateRunnableQueue().invokeLater(
            new Runnable()
            {
                @Override
                public void run()
                {
//                    Element clicked = (Element)event.getSource();
//                    System.out.print(clicked.toString());
                    //get the document SVG canvas is holding and get it's xml root element
                    Document a = _svgCanvas.getSVGDocument();
                    Element root = a.getDocumentElement();
                    
                    //add a new element to the root
                    Element text = a.createElementNS(NAMESPACE, "text");

                    text.setAttributeNS(null, "x", Integer.toString(point.x));
                    text.setAttributeNS(null, "y", Integer.toString(point.y));
                    //text.setAttributeNS(null, "fill", "#"+Integer.toHexString(_colorChooser.getColor().getRGB() & 0x00FFFFFF));
                    text.setTextContent(_anotationTextField.getText());

                    root.appendChild(text);
                }
            }
        );
    }
    
    
    @Override
    public void mousePressed(MouseEvent e) 
    {
        _dragStart = e.getPoint();
    }
    
    
    @Override
    public void mouseDragged(MouseEvent e)
    {
        UpdateManager updateManager = _svgCanvas.getUpdateManager();
        if(updateManager == null) return;
            final Point point = e.getPoint();
            updateManager.getUpdateRunnableQueue().invokeLater
            (
                new Runnable()
                {
                    @Override
                    public void run()
                    {
                        Document a = _svgCanvas.getSVGDocument();
                        Element root = a.getDocumentElement();
                        int x1 = _dragStart.x,
                                y1 = _dragStart.y,
                                x2 = point.x,
                                y2 = point.y;
                        
                        if(_tempRect != null)
                        {
                            root.removeChild(_tempRect);
                        }
                        boolean el = ((String)_shapeChooser.getSelectedItem()).equals("Ellipse");
                        Element rect = a.createElementNS(NAMESPACE, el?"ellipse":"rect");
                        
                        if(x1 > x2)
                        {
                            int p = x1;
                            x1 = x2;
                            x2 = p;
                        }
                        if(y1 > y2)
                        {
                            int p = y1;
                            y1 = y2;
                            y2 = p;  
                        }
                        rect.setAttributeNS(null, (el?"cx":"x"), (el?(x1+Math.abs(x2-x1)/2):x1)+"");
                        rect.setAttributeNS(null, (el?"rx":"width"), (el?(Math.abs(x2-x1)/2):Math.abs(x2-x1))+"");
                        
                        rect.setAttributeNS(null, (el?"cy":"y"), (el?(y1+Math.abs(y2-y1)/2):y1)+"");
                        rect.setAttributeNS(null, (el?"ry":"height"), (el?(Math.abs(y2-y1)/2):Math.abs(y2-y1))+"");

                        rect.setAttributeNS(null, "style", "fill:none;stroke:black;stroke-width:5");

                        _tempRect =  root.appendChild(rect);
                    }
                }
            );
    }
    
    
    
    @Override
    public void mouseReleased(MouseEvent e) {
        {
            
            //get update manager for the canvas
            UpdateManager updateManager = _svgCanvas.getUpdateManager();
            if(updateManager == null) return;
            final Point point = e.getPoint();
            
            if(point.x != _dragStart.x || point.y != _dragStart.y)
            {
                //run the update event for the svg canvas
                updateManager.getUpdateRunnableQueue().invokeLater
                (
                    new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            //get document root
                            Document a = _svgCanvas.getSVGDocument();
                            Element root = a.getDocumentElement();
                            if (_tempRect != null) {
                                root.removeChild(_tempRect);
                                _tempRect = null;
                            }
                            //add annotation
                            
                            a = _domManipulator.AddAnnotation(a,
                                    _colorChooser.getColor(), 
                                    _anotationTextField.getText(), 
                                    (String)_shapeChooser.getSelectedItem(),
                                    _includeText.isSelected(),
                                    _dragStart, 
                                    point);
                        }
                        
                    }
                );
            }
        }
    }
}
